package uit.mmt02.doituong;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

import javax.mail.Part;

import android.os.Environment;
import android.util.Log;

public class OneMail
{
	private int id;
    private String dateSend ; //ngay gui
    private String Sub ; // tieu de thu
    private String from ;//nguoi dui
    private String body ; // noi dung thu
    private String to;
    private boolean check;
    private boolean sao; // thu da gan sao hay chua
    private boolean flagAttach = false;
    private boolean flagVoice = false;
    private boolean flagEncrypt = false;
    String ChuoiAttach;
    
    public OneMail(int id, String from, String to, String sub, String body,
			String dateSend,  String ChuoiAttach,boolean sao,
			boolean flagEncrypt, boolean flagVoice) {
		super();
		this.id = id;
		this.from = from;
		this.to = to;
		Sub = sub;
		this.body = body;
		this.dateSend = dateSend;
		this.sao = sao;
		this.ChuoiAttach= ChuoiAttach;
		this.flagEncrypt = flagEncrypt;
		this.flagVoice = flagVoice;
	}


	public int getId() {
		return id;
	}


	public void setId(int id) {
		this.id = id;
	}

	
    public String getChuoiAttach() {
		return ChuoiAttach;
	}


	public void setChuoiAttach(String chuoiAttach) {
		ChuoiAttach = chuoiAttach;
	}
//    private int attach_number;
    
    private Part[] attach_parts;
//    private String[] attach_names;
    public OneMail()
    {
    	Sub = "OM sub";
    	from = "Om from";
    	to = "OM to";
    	body = "Om body";
    	dateSend = "Om daysend";
    	check = false;
    	sao = false;
    	flagAttach = true;
    	flagEncrypt = true;
    }
    
    
    public OneMail(String from, String dateSend, String Sub, String body) {
		super();
		this.from = from;
		this.dateSend = dateSend;
		this.Sub = Sub;
		this.body = body;
	}
    
    
    public boolean isFlagVoice() {
		return flagVoice;
	}


	public void setFlagVoice(boolean flagVoice) {
		this.flagVoice = flagVoice;
	}


	public boolean isFlagEncrypt() {
		return flagEncrypt;
	}


	public void setFlagEncrypt(boolean flagEncrypt) {
		this.flagEncrypt = flagEncrypt;
	}


	public Part[] getAttach_parts() {
		return attach_parts;
	}


	public void setAttach_parts(Part[] attach_parts) {
		this.attach_parts = attach_parts;
	}


	public boolean isFlagAttach() {
		return flagAttach;
	}


	public void setFlagAttach(boolean flagAttach) {
		this.flagAttach = flagAttach;
	}


	public String getSub() {
        return Sub;
    }

    public void setSub(String Sub) {
        this.Sub = Sub;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public String getDateSend() {
        return dateSend;
    }

    public void setDateSend(String dateSend) {
        this.dateSend = dateSend;
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

	public String getTo() {
		return to;
	}

	public void setTo(String to) {
		this.to = to;
	}

	public boolean isCheck() {
		return check;
	}

	public void setCheck(boolean check) {
		this.check = check;
	}

	public boolean isSao() {
		return sao;
	}

	public void setSao(boolean sao) {
		this.sao = sao;
	}
	public void set_attach_parts(Part[] parts){
		this.attach_parts = parts;
	}

	public Part[] get_attach_parts(){
		return attach_parts;
	}
	public void attach_down(Part[] attachpart, int i){
		try {
			String fileName = attachpart[i].getFileName();
			InputStream inputstream = attachpart[i].getInputStream();
			
				if(fileName==null){
					fileName = File.createTempFile("xxx", ".out").getName();
				}
				File savedir = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/downloaddir");
				savedir.mkdirs();
				File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/downloaddir",fileName);
				for(int j=0;file.exists();j++){
					savedir = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/downloaddir/"+fileName+j);
					savedir.mkdirs();
					file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/downloaddir/"+fileName+j,fileName);
				}
				
					BufferedOutputStream bufferOUT = new BufferedOutputStream(
							new FileOutputStream(file));

					BufferedInputStream bufferIN = new BufferedInputStream(inputstream);

					int abyte;
					while ((abyte = bufferIN.read()) != -1) {
						bufferOUT.write(abyte);
					}

					bufferOUT.flush();
					bufferOUT.close();
					bufferIN.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			Log.i("DOWN ATTACH", "ERROR WHILE GET ATTACHMENT"+e);
		}
	
	}


}
